﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace QuanLyDoiXe
{
    public partial class frmBaoCaoTinhTrangGiaoHang : Form
    {
        public frmBaoCaoTinhTrangGiaoHang()
        {
            InitializeComponent();
        }

        private string KiemTraDuLieu()
        {
            string chuoiloi = "";
            
            int x;
            if (Int32.TryParse(txtNam.Text.Trim(), out x) == false)
            {
                chuoiloi += "Năm phải là một số nguyên.\n";
            }
            return chuoiloi;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            string chuoiloi = KiemTraDuLieu();
            if (chuoiloi != "")
            {
                MessageBox.Show(chuoiloi);
                return;
            }
            string @thang = cboThang.SelectedItem.ToString();
            string @nam = txtNam.Text.Trim();

            string strQuery = "select distinct hd.biensoxegh, lh.tenloaihang, hd.ngaygiaohang, tt.tentrangthai ";
            strQuery += "FROM ((hoa_don_Giao_hang AS hd INNER JOIN chi_tiet_hoa_don_giao_hang AS ct ON hd.mahoadongh = ct.mahoadongh) INNER JOIN loai_hang AS lh ON ct.loaihang = lh.maloaihang) INNER JOIN trang_thai AS tt ON ct.matrangthai = tt.matrangthai";
            strQuery += " where (datepart('m',hd.NgayGiaoHang) = '" + @thang + "') AND (datepart('yyyy',hd.NgayGiaoHang) = '" + @nam + "')";
            string strCnn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QuanLyDoiXe.accdb";
            OleDbDataAdapter da = new OleDbDataAdapter(strQuery, strCnn);

            QuanLyDoiXeDataset dts = new QuanLyDoiXeDataset();
            da.Fill(dts, "TinhTrangGiaoHang");

            rptBaoCaoTinhTrangGiaoHang rptTinhTrangGH = new rptBaoCaoTinhTrangGiaoHang();
            rptTinhTrangGH.SetDataSource(dts);
            rptTinhTrangGH.SetParameterValue("@thang", @thang);
            rptTinhTrangGH.SetParameterValue("@nam", @nam);
            rptViewerTinhTrangGiaoHang.ReportSource = rptTinhTrangGH;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtNam_TextChanged(object sender, EventArgs e)
        {
            if (txtNam.Text.Trim() != "" )
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }

        private void frmBaoCaoTinhTrangGiaoHang_Load(object sender, EventArgs e)
        {
            btnDongY.Enabled = false;
            cboThang.SelectedIndex = 0;
        }
    }
}
